//
//  AccountDetailViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/9.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "AccountDetailViewController.h"
#import "AccountDetailTableViewCell.h"
@interface AccountDetailViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIView * headerView;
@property (nonatomic, strong)UILabel * accountContentLab;//具体余额
@property (nonatomic, strong)NSMutableArray * cellData;
@property (nonatomic, assign)int currentPage;
@end

@implementation AccountDetailViewController(networking)
- (void)getpayDetail{
    NSDictionary *parameters = @{
                                 @"userId":[UserModel sharedInstanced].userID,
                                 @"cookie":[UserModel sharedInstanced].cookie,
                                 @"page":@(self.currentPage),
                                 @"size":@(10)
                                 };
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"pay/getpayDetail" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"msg"] isEqualToString:@"success"]) {
            if (self.currentPage == 1) {
                [self.cellData removeAllObjects];
            }
            [self.cellData addObjectsFromArray:responseObject[@"list"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(@"msg");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation AccountDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"余额明细";
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.tableView];
    self.currentPage = 1;
    [self getpayDetail];//获取接口
}

#pragma mark - init
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - BOTTOM_HEIGHT - NAVI_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = 66/WIDTH_6_SCALE;
        [_tableView registerClass:[AccountDetailTableViewCell class] forCellReuseIdentifier:NSStringFromClass([AccountDetailTableViewCell class])];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.currentPage++;
            [self getpayDetail];
        }];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.currentPage = 1;
            [self getpayDetail];
        }];
    }return _tableView;
}
- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 123/WIDTH_6_SCALE)];
        _headerView.backgroundColor = getColor(@"f5f5f5");
        //背景
        UIImageView * bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 90/WIDTH_6_SCALE)];
        bgView.image = [UIImage imageNamed:@"maself_bg"];
        [_headerView addSubview:bgView];
        //余额标题
        UILabel * accountTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(18/WIDTH_6_SCALE, 20/WIDTH_6_SCALE, 100/WIDTH_6_SCALE, 15/WIDTH_6_SCALE)];
        [self label:accountTitleLab withFont:DEF_FontSize_14 withText:@"账户余额(元)" withTextColor:[UIColor whiteColor] withTextAlignment:NSTextAlignmentLeft withView:bgView withBGColor:[UIColor clearColor]];
        //余额具体
        self.accountContentLab = [[UILabel alloc] initWithFrame:CGRectMake(accountTitleLab.left, accountTitleLab.bottom + 14/WIDTH_6_SCALE, SCREEN_WIDTH - 36/WIDTH_6_SCALE, accountTitleLab.height)];
        [self label:self.accountContentLab withFont:DEF_FontSize_16 withText:self.accountContenStr withTextColor:[UIColor whiteColor] withTextAlignment:NSTextAlignmentLeft withView:bgView withBGColor:[UIColor clearColor]];
        //明细标题
        UILabel * detailTitleLab = [[UILabel alloc] initWithFrame:CGRectMake(self.accountContentLab.left, bgView.bottom, self.accountContentLab.width, _headerView.height - bgView.height)];
        [self label:detailTitleLab withFont:DEF_FontSize_12 withText:@"余额明细" withTextColor:getColor(@"9b9b9b") withTextAlignment:NSTextAlignmentLeft withView:_headerView withBGColor:[UIColor clearColor]];
    }return _headerView;
}
- (NSMutableArray *)cellData{
    if (!_cellData) {
        _cellData = @[].mutableCopy;
    }return _cellData;
}
#pragma mark - tableViewDelegate和tableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AccountDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AccountDetailTableViewCell class]) forIndexPath:indexPath];
    cell.model = self.cellData[indexPath.row];
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cellData.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}@end
